﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace HARTCalibrationTool.JsonFilesManager
{
    public class TransducersInfo
    {
        public string TransducerId { set; get; }
        public bool Status { set; get; }
        public uint LongAddress { set; get; }
        public string SN { set; get; }
    }

    public class ConfigInfo
    {
        [JsonProperty(Order = 1)]
        public string OrderSerialNumber { set; get; }

        [JsonProperty(Order = 2)]
        public int RangeUp { set; get; }

        [JsonProperty(Order = 3)]
        public int RangeDown { set; get; }

        [JsonProperty(Order = 4)]
        public string SensorRange { set; get; }

        [JsonProperty(Order = 5)]
        public byte EqualVal { set; get; }

        [JsonProperty(Order = 6)]
        public byte TemperaturePoints { set; get; }

        [JsonProperty(Order = 7)]
        public string Temperature1 { set; get; }
        [JsonProperty(Order = 8)]
        public string Temperature2 { set; get; }
        [JsonProperty(Order = 9)]
        public string Temperature3 { set; get; }
        [JsonProperty(Order = 10)]
        public string Temperature4 { set; get; }
        [JsonProperty(Order = 11)]
        public string Temperature5 { set; get; }
        [JsonProperty(Order = 12)]
        public string Temperature6 { set; get; }
        [JsonProperty(Order = 13)]
        public string Temperature7 { set; get; }
        [JsonProperty(Order = 14)]
        public string Temperature8 { set; get; }

        [JsonProperty(Order = 15)]
        public byte PressureFitDegree;

        [JsonProperty(Order = 16)]
        public byte TemperatureFitDegree;

        [JsonProperty(Order = 17)]
        public double PressureFluctuationThreshold;

        [JsonProperty(Order = 18)]
        public double PressureLinearityErrThreshold;

        [JsonProperty(Order = 19)]
        public Dictionary<string, Dictionary<string, bool>> TemperatureCollectionStatus { get; set; }

        [JsonProperty(Order = 20)]
        public bool[,] TemperatureCaliStatus;

        [JsonProperty(Order = 21)]
        public TransducersInfo[] transducersInfo;

        public ConfigInfo()
        {
            TemperatureCaliStatus = new bool[6, 9]
            {
                { false, false, false, false, false, false, false, false, false}, // temperature1
                { false, false, false, false, false, false, false, false, false}, // temperature2
                { false, false, false, false, false, false, false, false, false}, // temperature3
                { false, false, false, false, false, false, false, false, false}, // temperature4
                { false, false, false, false, false, false, false, false, false}, // temperature5
                { false, false, false, false, false, false, false, false, false}, // temperature6
            };

            TemperatureCollectionStatus = new Dictionary<string, Dictionary<string, bool>>()
            {
                {
                    "temperature1", 
                    new Dictionary<string, bool>() 
                    {
                        { "UP1", false}, { "DP1", false},                        
                        { "UP2", false}, { "DP2", false},
                        { "UP3", false}, { "DP3", false},
                        { "UP4", false}, { "DP4", false},
                        { "UP5", false}, { "DP5", false},
                        { "UP6", false}, { "DP6", false},
                        { "UP7", false}, { "DP7", false},
                        { "UP8", false}, { "DP8", false},
                        { "UP9", false}, { "DP9", false},
                    }
                },
                {
                    "temperature2", 
                    new Dictionary<string, bool>() 
                    {
                        { "UP1", false}, { "DP1", false},                        
                        { "UP2", false}, { "DP2", false},
                        { "UP3", false}, { "DP3", false},
                        { "UP4", false}, { "DP4", false},
                        { "UP5", false}, { "DP5", false},
                        { "UP6", false}, { "DP6", false},
                        { "UP7", false}, { "DP7", false},
                        { "UP8", false}, { "DP8", false},
                        { "UP9", false}, { "DP9", false},
                    }
                },
                {
                    "temperature3", 
                    new Dictionary<string, bool>() 
                    {
                        { "UP1", false}, { "DP1", false},                        
                        { "UP2", false}, { "DP2", false},
                        { "UP3", false}, { "DP3", false},
                        { "UP4", false}, { "DP4", false},
                        { "UP5", false}, { "DP5", false},
                        { "UP6", false}, { "DP6", false},
                        { "UP7", false}, { "DP7", false},
                        { "UP8", false}, { "DP8", false},
                        { "UP9", false}, { "DP9", false},
                    }
                },
                {
                    "temperature4", 
                    new Dictionary<string, bool>() 
                    {
                        { "UP1", false}, { "DP1", false},                        
                        { "UP2", false}, { "DP2", false},
                        { "UP3", false}, { "DP3", false},
                        { "UP4", false}, { "DP4", false},
                        { "UP5", false}, { "DP5", false},
                        { "UP6", false}, { "DP6", false},
                        { "UP7", false}, { "DP7", false},
                        { "UP8", false}, { "DP8", false},
                        { "UP9", false}, { "DP9", false},
                    }
                },
                {
                    "temperature5", 
                    new Dictionary<string, bool>() 
                    {
                        { "UP1", false}, { "DP1", false},                        
                        { "UP2", false}, { "DP2", false},
                        { "UP3", false}, { "DP3", false},
                        { "UP4", false}, { "DP4", false},
                        { "UP5", false}, { "DP5", false},
                        { "UP6", false}, { "DP6", false},
                        { "UP7", false}, { "DP7", false},
                        { "UP8", false}, { "DP8", false},
                        { "UP9", false}, { "DP9", false},
                    }
                },
                {
                    "temperature6", 
                    new Dictionary<string, bool>() 
                    {
                        { "UP1", false}, { "DP1", false},                        
                        { "UP2", false}, { "DP2", false},
                        { "UP3", false}, { "DP3", false},
                        { "UP4", false}, { "DP4", false},
                        { "UP5", false}, { "DP5", false},
                        { "UP6", false}, { "DP6", false},
                        { "UP7", false}, { "DP7", false},
                        { "UP8", false}, { "DP8", false},
                        { "UP9", false}, { "DP9", false},
                    }
                },
            };

            transducersInfo = new TransducersInfo[32]
            {
                new TransducersInfo
                {
                    TransducerId = "变送器1",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器2",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器3",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器4",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器5",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器6",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器7",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器8",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器9",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器10",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器11",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器12",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器13",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器14",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器15",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器16",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器17",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器18",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器19",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器20",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器21",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器22",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器23",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器24",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器25",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器26",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器27",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器28",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器29",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器30",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器31",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
                new TransducersInfo
                {
                    TransducerId = "变送器32",
                    Status = false,
                    LongAddress = 0,
                    SN = null,
                },
            };
        }
    }

    public class JsonConfigInfo : JsonBase
    {
        public static ConfigInfo configInfo { set; get; } = new ConfigInfo();

        public JsonConfigInfo()
        {

        }

        public static bool JsonFileSaveInfo(string fileName)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();

            var settings = new JsonSerializerSettings
            {
                Formatting = Formatting.Indented,
                NullValueHandling = NullValueHandling.Include,
            };

            var jsonString = JsonConvert.SerializeObject(configInfo, settings);

            fileDialog.FileName = fileName;
            fileDialog.Filter = "JSON file (*.json) | *.json";
            fileDialog.InitialDirectory = initialDirectory;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                initialDirectory = fileDialog.FileName;

                File.WriteAllText(initialDirectory, jsonString);

                return true;
            }
            else
            {
                return false;
            }
        }

        public static ConfigInfo JsonLoadInfo(string filePath)
        {
            string jsonString = File.ReadAllText(filePath);

            ConfigInfo configInfo = JsonConvert.DeserializeObject<ConfigInfo>(jsonString);

            return configInfo;
        }
    }
}
